import { intl } from "@/utils";
import { ExclamationCircleOutlined } from "@ant-design/icons";
import { Input, message, Modal, ModalFuncProps, Space } from "antd";

const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));

/**
 * 输入对话框
 * @param valueType 类型 作废原因
 * @param defaultValue 默认值
 * @param okCallBack ok按钮回调,返回true关闭,false停留在当前对话框
 */
export function inputQuery(title: string | any, valueType: string, defaultValue: string,
      okCallBack: (value: string) => Promise<boolean | undefined>,
      promptText: any, options: any) {

      let loading: boolean = false;
      let inputRefCurrent: any; // initialState.projectCodeInputRef;
      let placeholder = intl('pub.pleaseInput', '请输入') + valueType;
      let modalOptions: ModalFuncProps = {
            title: <div style={{ fontSize: 14 }}>{title}</div>,
            icon: <ExclamationCircleOutlined />,
            // centered: true,
            content:
                  <>
                        <Space direction="horizontal">
                              <Input
                                    size="middle"
                                    style={{ width: 320 }}
                                    ref={(current: any) => { inputRefCurrent = current; }}
                                    placeholder={placeholder} defaultValue={defaultValue}
                                    onKeyUp={async (e: any) => {
                                          if (e.keyCode == 13) { //回车
                                                await onOkClick(e);
                                          }
                                    }}
                              />

                        </Space>
                        {promptText}
                  </>,
            okText: intl('dialog.confirm', '确认'),
            cancelText: intl('dialog.cancel', '取消'),
            onOk: (e) => {
                  onOkClick(e)
            },
            okButtonProps: { loading: loading, disabled: loading },
            cancelButtonProps: { loading: loading, disabled: loading },

            ...options,
      };

      let modal = Modal.confirm(modalOptions);
      async function onOkClick(e: any) {
            let value = inputRefCurrent?.input?.value;

            if (!value) {
                  message.error(intl('pub.thisItemCantEmpty', '{name}不能为空,请输入{name}!', { name: valueType || '' }));
                  inputRefCurrent?.input?.focus();
                  return new Promise((resolve, reject) => { reject(); });
            }
            let isOK = okCallBack && await okCallBack(value);
            if (isOK == false) {
                  return new Promise((resolve, reject) => { reject(); });
            }
            return modal.destroy();
      }
}
//显示消息对话框
export function showMessageInfo(title: string, content: string | any, options?: any) {
      Modal.info({
            title: title,
            content: (
                  <div>
                        {content}
                  </div>
            ),
            onOk() { },
            centered: true,
            zIndex: 9800,
            okText: intl('pub.confirm', '确定'),
            ...options,
      });
}
export function showErrorInfo(title: string, content: string | any, options?: any) {
      Modal.error({
            title: title,
            content: (
                  <div>
                        {content}
                  </div>
            ),
            onOk() { },
            centered: true,
            zIndex: 9800,
            okText: intl('pub.confirm', '确定'),
            ...options,
      });
}